package com.example.pharmacy.interceptor;

import com.example.pharmacy.commen.Constant;
import com.example.pharmacy.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-25
 * Time: 20:43
 */
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("登录拦截器");
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute(Constant.SESSION_USER_KEY);
        if (name != null) {
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
